package com.yl.auth.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yl.auth.entity.Fangjian;
import com.yl.auth.entity.Huligenghuanshenpi;
import com.yl.auth.model.entity.Chuangwei;
import com.yl.auth.model.entity.HuLiDengJiBianGeng;
import com.yl.auth.model.entity.Laoren;
import com.yl.auth.model.vo.HuLiDengJiBianGengVo;
import com.yl.auth.service.*;
import com.yl.common.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author L
 * @since 2023-07-04
 */
@RestController
@RequestMapping("/admin/yanglao/hulidengjibiangeng")
@Api(tags = "市场销售-护理等级变更管理")
public class HuLiDengJiBianGengController {

    @Resource
    private HuLiDengJiBianGengService huLiDengJiBianGengService;

    @Resource
    private ChuangweiService chuangweiService;

    @Resource
    private FangjianService fangjianService;

    @Resource
    private LaorenService laorenService;
    @Resource
    private HuligenghuanshenpiService huligenghuanshenpiService;
    @ApiOperation("全查+分页+条件查询")
    @GetMapping("/findAllConPage/{page}/{limit}")
    public Result findAllConPage(@PathVariable Integer page,
                                 @PathVariable Integer limit,
                                 HuLiDengJiBianGengVo huLiDengJiBianGengVo){
        Page<HuLiDengJiBianGeng> page1 = new Page<>(page,limit);
        Page<HuLiDengJiBianGeng> huLiDengJiBianGengPage = huLiDengJiBianGengService.findAllConPage(page1,huLiDengJiBianGengVo);
       return  Result.ok(huLiDengJiBianGengPage);
    }

    @ApiOperation("审批详情")
    @GetMapping("/getById/{id}")
    public Result getById(@PathVariable Long id){
        //根据id获取护理变更信息
        HuLiDengJiBianGeng huLiDengJiBianGeng = huLiDengJiBianGengService.getById(id);
        //根据护理变更实体类中的老人外键获取到该老人信息
        Long laorenId = huLiDengJiBianGeng.getLaorenId();
        Laoren laoren = laorenService.getById(laorenId);
        //根据老人中的床位外键获取到该老人的床位信息
        Chuangwei chuangwei = chuangweiService.getById(laoren.getBedId());
        //根据老人中的房间外键获取到该老人指定的房间信息
        Fangjian fangjian = fangjianService.getById(laoren.getRoomId());
        //对获取到的房间号进行分割,循环取到房间号的第一个值
        String[] split = fangjian.getRoomNo().toString().split("");
        String str = "";
        for (int i = 0; i < split.length; i++) {
            str = split[0];
        }
        //获取到该老人房间信息
        huLiDengJiBianGeng.setChuangWeiHuLi(str+"楼"+fangjian.getRoomNo()+"室");
        //获取到该老人床位信息
        huLiDengJiBianGeng.setChuangWeiBed(fangjian.getRoomNo()+"-"+chuangwei.getBedNo());
        Map<String,Object> map = new HashMap<>();
        map.put("huLiDengJiBianGeng",huLiDengJiBianGeng);
        map.put("laoren",laoren);
        return Result.ok(map);
    }


    @ApiOperation("新增中回显老人信息")
    @GetMapping("/getLaoRen")
    public Result getLaoRen(){
        LambdaQueryWrapper<Laoren> laorenLambdaQueryWrapper = new LambdaQueryWrapper<>();
        laorenLambdaQueryWrapper.eq(Laoren::getStatus,2);
        List<Laoren> list = laorenService.list(laorenLambdaQueryWrapper);
        return Result.ok(list);
    }

    @ApiOperation("新增护理变更申请")
    @PostMapping("/save")
    public Result save(@RequestBody HuLiDengJiBianGeng huLiDengJiBianGeng){
        huLiDengJiBianGeng.setPutindate(new Date());
        huLiDengJiBianGeng.setState(0);
        huLiDengJiBianGengService.save(huLiDengJiBianGeng);
        return Result.ok();
    }

    @ApiOperation("编辑回显信息")
    @GetMapping("/getByHuId/{id}")
    public Result getByHuId(@PathVariable Long id){
        HuLiDengJiBianGeng huLiDengJiBianGeng = huLiDengJiBianGengService.getById(id);
        Long laorenId = huLiDengJiBianGeng.getLaorenId();
        LambdaQueryWrapper<Laoren> laorenLambdaQueryWrapper = new LambdaQueryWrapper<>();
        laorenLambdaQueryWrapper.eq(Laoren::getId,laorenId);
        List<Laoren> list = laorenService.list(laorenLambdaQueryWrapper);
        Laoren laoren = laorenService.getById(laorenId);
        System.out.println(laoren.toString());
        Map<String,Object> map = new HashMap<>();
        map.put("huLiDengJiBianGeng",huLiDengJiBianGeng);
        map.put("laoren",laoren);
        map.put("list",list);
        return Result.ok(map);
    }
    @ApiOperation("编辑护理变更申请")
    @PostMapping("/update")
    public Result huUpdate(@RequestBody HuLiDengJiBianGeng huLiDengJiBianGeng) {
        System.out.println(huLiDengJiBianGeng);
        huLiDengJiBianGeng.setState(0);
        huLiDengJiBianGengService.updateById(huLiDengJiBianGeng);
        return Result.ok();
    }

    @ApiOperation("通过老人id获取到老人姓名")
    @GetMapping("/getLaoRenShenPiById/{id}")
    public Result getLaoRenShenPiById(@PathVariable Long id){
        String name = huLiDengJiBianGengService.getLaoRenShenPiById(id);
        return Result.ok(name);
    }

    @ApiOperation("根据护理等级变更申请的id再获取到该老人信息以及变更后的合同")
    @GetMapping("/getLaorenBianGeng/{id}")
    public Result getLaorenBianGeng(@PathVariable Long id){
        HuLiDengJiBianGeng huLiDengJiBianGeng = huLiDengJiBianGengService.getById(id);
        Long laorenId = huLiDengJiBianGeng.getLaorenId();
        LambdaQueryWrapper<Huligenghuanshenpi> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Huligenghuanshenpi::getLaorenId,laorenId);
        Huligenghuanshenpi one = huligenghuanshenpiService.getOne(lambdaQueryWrapper);
        return Result.ok(one);
    }

    @ApiOperation("拒绝编辑后开始流程将审批表拒绝信息删除")
    @DeleteMapping("/deleteById/{id}")
    public Result deleteById(@PathVariable Integer id){
        LambdaQueryWrapper<Huligenghuanshenpi> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Huligenghuanshenpi::getLaorenId,id);
        huligenghuanshenpiService.remove(lambdaQueryWrapper);
        return Result.ok();
    }
}
